// components/person/myBaby.js
import Toast from '@vant/weapp/toast/toast';
Component({
  /**
   * 组件的属性列表
   */
  properties: {


  },

  /**
   * 组件的初始数据
   */
  data: {
    imgPath: '/images',

    //个人主页信息
    home: null,
    //陪伴天数
    daysOfCompanionship: 0,
    //是否显示背景
    showBg: true,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    addBaby() {
      wx.navigateTo({
        url: '/pages/service/tianXieBaoBao',
      })
    },
    /**
     * 陪伴成长天数
     * @param {*} home 
     */
    calDays(home) {
      //陪伴成长天数
      if(!home || !home.member_card_info || !home.member_card_info.start_time) return;
     // console.log("home", home)
      let dtNow=new Date();
      let card_start_time = new Date(home.member_card_info.start_time);
      let card_diff = Number(dtNow) - Number(card_start_time);
      let card_diff_days = Math.round(Number(card_diff) / (24 * 60 * 60 * 1000));
      this.setData({
        daysOfCompanionship: home.member_card_info.defend_baby_days,
      })
    }
  },
  lifetimes: {
    attached() {
      const that = this;

      const util = require("../../utils/util")
      this.setData({
        imgPath: util.getImgPath()
      })
      // Toast.loading({
      //   message: '加载中...',
      //   forbidClick: true,
      // });

      let home = wx.getStorageSync('home');
      if (home) {
        that.setData({
          home: home,
          showBg: false,
        })

        //计算陪伴天数
        this.calDays(home)
      } else {

        //取得个人中心信息
        const api = require("../../api/api")
        api.user.home({
          success(res) {
            // console.log("home,res", res)
            // res.data.data.has_baby=0;
            that.setData({
              home: res.data.data,
              showBg: false,
            })

            //保存home到缓存
            wx.setStorageSync('home', res.data.data);

            //计算陪伴天数
            this.calDays(res.data.data)
          }
        })
      }
    }
  }
})